#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtHeBeiWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}/notice/home'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#keyword', '#buttonSearch', keyword,
                                               'div.contentA div.contentA1 p',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            if jq.find('div.contentA1').find('p').find('span').text() == '0':
                return param_list, self.SEARCH_NOTHING_FIND

            pattern = 'http://he\.gsxt\.gov\.cn/notice/notice/view\?uuid=(.*?)&amp;tab=01'
            regex = re.compile(pattern)
            search_list = regex.findall(content)
            param_set = set()
            if len(search_list) > 0:
                for item in search_list:
                    if item in param_set:
                        continue

                    param_list.append({'uuid': item})
                    param_set.add(item)
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    @staticmethod
    def __get_company_name(text):
        search_list = re.findall('<li class=\"titleB\"><em title=\".*?\">(.*?)</em>', text)
        if len(search_list) > 0:
            return search_list[0].strip()

        return None

    def get_shareholder_info(self, session, uuid, data):
        url = 'http://{host}/notice/notice/view?uuid={uuid}&tab=02'.format(
            host=self.host, uuid=uuid)
        r = self.task_request(session.get, url)
        if r is None:
            return None, None
        self.append_model(data, Model.shareholder_info, url, r.text)

        return url, r.text

    def get_base_info(self, session, uuid):
        url = 'http://{host}/notice/notice/view?uuid={uuid}&tab=01'.format(
            host=self.host, uuid=uuid)

        # 基本信息
        r = self.task_request(session.get, url)
        if r is None:
            return None, None

        return url, r.text

    # 主要人员
    def get_key_person_info(self, session, uuid, data):
        url = 'http://{host}/notice/notice/moreMember?uuid={uuid}'.format(
            host=self.host, uuid=uuid)
        r = self.task_request(session.get, url)
        if r is None:
            return None, None
        self.append_model(data, Model.key_person_info, url, r.text)

        return url, r.text

    # 获得分支机构信息
    def get_branch_info(self, session, uuid, data):
        url = 'http://{host}/notice/notice/moreBranch?uuid={uuid}'.format(
            host=self.host, uuid=uuid)
        r = self.task_request(session.get, url)
        if r is None:
            return None, None
        self.append_model(data, Model.branch_info, url, r.text)

        return url, r.text

    # 获得年报信息
    def get_annual_info(self, session, text, data):
        if text == '' or text is None:
            return
        try:
            jq = PyQuery(text, parser='html')
            item_list = jq.find('tr').items()
            for item in item_list:
                if item.text().find('年度报告') == -1:
                    continue

                year_info = item.find('td').eq(1).text()
                year_list = re.findall('\d+', year_info)
                if len(year_list) <= 0:
                    continue

                year = str(year_list[0])

                url = item.find('a').attr('href')
                if url is None or url == '':
                    continue

                if url.find('view_annual') == -1:
                    continue

                r = self.task_request(session.get, url)
                if r is None:
                    continue
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)
        except Exception as e:
            self.log.exception(e)

    def get_detail_html_list(self, seed, session, param_list):
        name_error_count = 0
        # 保存企业名称
        data_list = []
        rank = 0
        for item in param_list:
            try:
                uuid = item.get('uuid', None)
                url, text = self.get_base_info(session, uuid)
                if url is None:
                    continue

                # 获得公司名称
                company_name = self.__get_company_name(text)
                if company_name is None or company_name == '':
                    self.log.error('公司名称解析失败..uuid = {uuid} {text}'.format(
                        uuid=uuid, text=text))
                    name_error_count += 1
                    continue

                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)

                # 存储数据
                self.append_model(data, Model.base_info, url, text)

                # 主要人员
                self.get_key_person_info(session, uuid, data)

                # 分支机构
                self.get_branch_info(session, uuid, data)

                # 股东信息
                url, text = self.get_shareholder_info(session, uuid, data)

                # 年报信息
                self.get_annual_info(session, text, data)

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)

        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list) + name_error_count
